package com.placement.app.action;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.placement.app.exception.JobRuntimeException;
import com.placement.app.forms.BaseForm;
import com.placement.app.forms.UserForm;
import com.placement.app.util.Constants;
import com.placement.app.util.JobProperties;

/**
 * Purpose: This class is Abstract DispatchAction class each action class in application extends BaseAction.
 * 			
 * 
 *
 */
public class BaseAction extends DispatchAction {
	private static final Log logger = LogFactory.getLog(BaseAction.class);
	
	private String action;

	public void setAction(String action) {
		this.action = action;
	}
	public String getAction() {
		return action;
	}
	
	/** Retrieves userid from the Session
	 * @param request
	 * @return 	
	 */
	public final String getLoggedInUser(HttpServletRequest request)
	{
		String user="1";
		HttpSession session=request.getSession();
		
		if(session!=null)
		{
			user=(String)session.getAttribute(Constants.SES_USER_ID);
			if(user!=null)
				logger.info("User Id found in SESSION  :" + user);
		}
		
		if(user==null)
			{
			/*
				//check if app should use hard coded user id or not 
				String takeHardCodedUser=(String)JobProperties.getProperty(Constants.PROPERTY_USE_JOB_USER_ID);
				if(takeHardCodedUser!=null && takeHardCodedUser.equals("1"))
				{
					logger.info("No header found. Retriving hardcoded user id from property file:");					
					user=(String)JobProperties.getProperty(Constants.PROPERTY_JOB_USER_ID);
					if(user==null || user.trim().equals(""))
						user="";
					logger.info("Retrived hardcoded user id from property file as:"+user);
					if(user!=null)
					{
						logger.info("Checking if user has access to Job application or not");
						
					}
				}
				
				//If app is not supposed to hard coded user id and still User Id is null then throw error
				if((takeHardCodedUser==null || takeHardCodedUser.equals("0"))|| (user==null || user.equals("")))
				{
					throw new JobRuntimeException("User Id is neither found in JANUS HEADER nor in Job property file. Try Log in again");
				}
			}
		
		logger.info("Logged in user is  :" + user);
		session.setAttribute(Constants.SES_USER_ID,user);
		return user;
		*/
						}
		return user;	
	}
	
		
	public ActionForward logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request,	HttpServletResponse response) 
	{
		String userId = getLoggedInUser(request);
		String url = JobProperties.getProperty("logout.url");
		request.setAttribute("url", url);
		logger.debug("Logout url: " + url);
		
		RequestDispatcher rd = request.getRequestDispatcher("/pages/logout.jsp");
		try
		{
			HttpSession s = request.getSession();
			// the following line will invoke SrgtSessionListener.sessionDestroyed()
			s.setAttribute(Constants.SES_USER_ID, userId);
			s.invalidate();
			//proposalService.unlockProposal(null, userId);
			logger.info("User " + userId + " logged out successfully");
			rd.forward(request, response);
		}
		catch(Exception e)
		{
			logger.error("Failed to log out user " + userId + ". Error is:\n" + e.toString());
		}
		return null;
//		return mapping.findForward("/pages/logout.jsp");
	}
	
		
}	
	
